package StackandQueue;

import java.util.Stack;

/**
 * 用栈模拟队列
 */
public class MyQueue {
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;

    public MyQueue(){
        stackIn = new Stack<>();
        stackOut = new Stack<>();
    }
    //将元素x放入队列末尾
    public void push(int x){
        stackIn.push(x);
    }
    //将队列首部元素弹出并返回该元素的值
    public int pop(){
        dumpstackIn();
        return stackOut.pop();
    }
    //得到队列首部的元素，但不弹出
    public int peek(){
        dumpstackIn();
        return stackOut.peek();
    }
    //返回队列是否为空
    public boolean empty(){
        return stackIn.isEmpty() && stackOut.isEmpty();
    }

    //如果stackOut为空，将stackIn中的元素全部放到stackOut中
    private void dumpstackIn(){
        if (!stackOut.isEmpty()) return;
        while (!stackIn.isEmpty()){
            stackOut.push(stackIn.pop());
        }
    }
}
